<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="序号" prop="serialNum">
      <el-input v-model="dataForm.serialNum" placeholder="序号"></el-input>
    </el-form-item>
    <el-form-item label="类别" prop="category">
      <el-input v-model="dataForm.category" placeholder="类别"></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型"></el-input>
    </el-form-item>
    <el-form-item label="型号" prop="model">
      <el-input v-model="dataForm.model" placeholder="型号"></el-input>
    </el-form-item>
    <el-form-item label="cpu主频" prop="cpuMainFrequency">
      <el-input v-model="dataForm.cpuMainFrequency" placeholder="cpu主频"></el-input>
    </el-form-item>
    <el-form-item label="操作系统版本" prop="osVersion">
      <el-input v-model="dataForm.osVersion" placeholder="操作系统版本"></el-input>
    </el-form-item>
    <el-form-item label="内存" prop="memory">
      <el-input v-model="dataForm.memory" placeholder="内存"></el-input>
    </el-form-item>
    <el-form-item label="厂家联系方式" prop="contactInformation">
      <el-input v-model="dataForm.contactInformation" placeholder="厂家联系方式"></el-input>
    </el-form-item>
    <el-form-item label="最高限价" prop="priceCeiling">
      <el-input v-model="dataForm.priceCeiling" placeholder="最高限价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          serialNum: '',
          category: '',
          type: '',
          model: '',
          cpuMainFrequency: '',
          osVersion: '',
          memory: '',
          contactInformation: '',
          priceCeiling: ''
        },
        dataRule: {
          serialNum: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          category: [
            { required: true, message: '类别不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          model: [
            { required: true, message: '型号不能为空', trigger: 'blur' }
          ],
          cpuMainFrequency: [
            { required: true, message: 'cpu主频不能为空', trigger: 'blur' }
          ],
          osVersion: [
            { required: true, message: '操作系统版本不能为空', trigger: 'blur' }
          ],
          memory: [
            { required: true, message: '内存不能为空', trigger: 'blur' }
          ],
          contactInformation: [
            { required: true, message: '厂家联系方式不能为空', trigger: 'blur' }
          ],
          priceCeiling: [
            { required: true, message: '最高限价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/aubaseproducts/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.serialNum = data.aubaseproducts.serialNum
                this.dataForm.category = data.aubaseproducts.category
                this.dataForm.type = data.aubaseproducts.type
                this.dataForm.model = data.aubaseproducts.model
                this.dataForm.cpuMainFrequency = data.aubaseproducts.cpuMainFrequency
                this.dataForm.osVersion = data.aubaseproducts.osVersion
                this.dataForm.memory = data.aubaseproducts.memory
                this.dataForm.contactInformation = data.aubaseproducts.contactInformation
                this.dataForm.priceCeiling = data.aubaseproducts.priceCeiling
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/aubaseproducts/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'serialNum': this.dataForm.serialNum,
                'category': this.dataForm.category,
                'type': this.dataForm.type,
                'model': this.dataForm.model,
                'cpuMainFrequency': this.dataForm.cpuMainFrequency,
                'osVersion': this.dataForm.osVersion,
                'memory': this.dataForm.memory,
                'contactInformation': this.dataForm.contactInformation,
                'priceCeiling': this.dataForm.priceCeiling
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
